package bist.believeinstep.impl;

import java.util.List;

import javax.sql.DataSource;

import org.springframework.jdbc.core.JdbcTemplate;

import bist.believeinstep.dao.ConnectionDAO;
import bist.believeinstep.dao.MenuDAO;
import bist.believeinstep.domain.Menu;
import bist.believeinstep.mapper.MenuMapper;

public class MenuTemplate implements MenuDAO, ConnectionDAO{
	
	private JdbcTemplate jdbcTemplate;
	
	@Override
	public void setDataSource(DataSource dataSource){
		this.jdbcTemplate = new JdbcTemplate(dataSource);
	}
	@Override
	public void create(Menu menu){
		String query = "INSERT INTO menus (name,created_date, modified_date, created_by, modified_by) "
				+ "VALUES (?,?,?,?,?)";
		try{
			jdbcTemplate.update(query, new Object[]{
					menu.getName(),
					menu.getCreated(),
					menu.getModified(),
					menu.getCreatedBy(),
					menu.getModifiedBy()
			});
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	@Override
	public void update(Menu menu){
		String query = "UPDATE bist_menus SET"
				+ "name = ?"
				+ "created_date = ?"
				+ "modified_date = ?"
				+ "created_by = ?"
				+ "modified_by = ?";
		try{
			jdbcTemplate.update(query, new Object[]{
				menu.getName(),
				menu.getCreated(),
				menu.getModified(),
				menu.getCreatedBy(),
				menu.getModifiedBy()
			});
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	@Override
	public void delete(Integer id){
		String query = "DELETE FROM bist_menus where id = ?";
		try{
			jdbcTemplate.update(query, new Object[]{id});
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	@Override
	public Menu getMenu(Integer id){
		String query = "SELECT FROM bist_menus WHERE id = ?";
		try{
			Menu menu = jdbcTemplate.queryForObject(query, new Object[]{id}, new MenuMapper());
			return menu;
		}catch(Exception ex){
				ex.printStackTrace();			
		}
		return null;
	}
	@Override
	public Menu getMenu(String name){
		String query = "SELECT FROM bist_menus WHERE name = ?";
		try{
			Menu menu = jdbcTemplate.queryForObject(query, new Object[]{name}, new MenuMapper());
			return menu;
		}catch(Exception ex){
				ex.printStackTrace();			
		}
		return null;
	}
	@Override
	public List<Menu> list(){
		String query = "SELECT * FROM bist_menus";
		try{
			List<Menu> menu = jdbcTemplate.query(query, new MenuMapper());
			return menu;
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}

}
